import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useRef, useState } from "react";
import BScroll from "@better-scroll/core";
import MouseWheel from "@better-scroll/mouse-wheel";
import PullDown from "@better-scroll/pull-down";
import PullUp from "@better-scroll/pull-up";
import classNames from "classnames";
import classes from "./scrollView.module.less";
import { useControllableValue, useLatest, useMount, useSize, useUnmount, useUpdateEffect } from "ahooks";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
BScroll.use(MouseWheel);
BScroll.use(PullDown);
BScroll.use(PullUp);
export var isMobile = function() {
    var flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
    return flag;
};
var DEFAULT_PULL_DOWN_TEXTS_MAP = {
    enter: "Pull down",
    leave: "Release",
    fetching: "Loading...",
    succeed: "Refresh succeed"
};
var DEFAULT_PULL_UP_TEXTS_MAP = {
    fetching: "Loading...",
    succeed: "Pull up and load more"
};
var defaultPullUpTipRender = function(status) {
    return /*#__PURE__*/ _jsx("div", {
        className: classes.pullup_tip,
        children: DEFAULT_PULL_UP_TEXTS_MAP[status]
    });
};
var defaultPullDownTipRender = function(status) {
    return /*#__PURE__*/ _jsx("div", {
        className: classes.pulldown_tip,
        children: DEFAULT_PULL_DOWN_TEXTS_MAP[status]
    });
};
function ScrollView(props) {
    var lastProps = useLatest(props);
    var className = props.className, wrapperClassName = props.wrapperClassName, _props_renderPullDownTip = props.renderPullDownTip, renderPullDownTip = _props_renderPullDownTip === void 0 ? defaultPullDownTipRender : _props_renderPullDownTip, _props_renderPullUpTip = props.renderPullUpTip, renderPullUpTip = _props_renderPullUpTip === void 0 ? defaultPullUpTipRender : _props_renderPullUpTip, loadMore = props.loadMore, loading = props.loading, loadMoreLoading = props.loadMoreLoading, reload = props.reload, scrollX = props.scrollX, _props_scrollY = props.scrollY, scrollY = _props_scrollY === void 0 ? scrollX ? false : true : _props_scrollY, propsNoMore = props.noMore, _props_mouseWheel = props.mouseWheel, mouseWheel = _props_mouseWheel === void 0 ? true : _props_mouseWheel, _props_click = props.click, click = _props_click === void 0 ? true : _props_click, onPullingUp = props.onPullingUp, onNoMoreChange = props.onNoMoreChange, resetProps = _object_without_properties(props, [
        "className",
        "wrapperClassName",
        "renderPullDownTip",
        "renderPullUpTip",
        "loadMore",
        "loading",
        "loadMoreLoading",
        "reload",
        "scrollX",
        "scrollY",
        "noMore",
        "mouseWheel",
        "click",
        "onPullingUp",
        "onNoMoreChange"
    ]);
    var bScroll = useRef();
    var $content = useRef(null);
    var $wrapper = useRef(null);
    var _useControllableValue = _sliced_to_array(useControllableValue({
        noMore: !!propsNoMore,
        onNoMoreChange: onNoMoreChange
    }, {
        valuePropName: "noMore",
        trigger: "onNoMoreChange",
        defaultValue: false
    }), 2), noMore = _useControllableValue[0], setNoMore = _useControllableValue[1];
    var _useState = _sliced_to_array(useState("enter"), 2), pullDownStatus = _useState[0], setPullDownStatus = _useState[1];
    var _useState1 = _sliced_to_array(useState("succeed"), 2), pullUpStatus = _useState1[0], setPullUpStatus = _useState1[1];
    var init = function() {
        if (!$wrapper.current) return;
        bScroll.current = new BScroll($wrapper.current, _object_spread_props(_object_spread({}, resetProps), {
            scrollX: scrollX,
            scrollY: scrollY,
            useTransition: isMobile() ? false : true,
            // useTransition:false,
            click: click,
            mouseWheel: isMobile() ? mouseWheel : {
                dampingFactor: 0
            },
            pullDownRefresh: isMobile() ? resetProps.pullDownRefresh : undefined,
            pullUpLoad: resetProps.pullUpLoad
        }));
        if (resetProps.pullUpLoad && bScroll.current && (loadMore || onPullingUp)) {
            bScroll.current.on("pullingUp", function() {
                console.log(noMore);
                if (lastProps.current.noMore) {
                    bScroll.current.finishPullUp();
                    return;
                }
                setPullUpStatus("fetching");
                if (onPullingUp) {
                    onPullingUp === null || onPullingUp === void 0 ? void 0 : onPullingUp().then(function(hasNext) {
                        console.log(hasNext, "pulling");
                        if (hasNext) {
                            setNoMore(false);
                        } else {
                            setNoMore(true);
                        }
                    }).finally(function() {
                        setPullUpStatus("succeed");
                        bScroll.current.finishPullUp();
                    });
                    return;
                }
                loadMore === null || loadMore === void 0 ? void 0 : loadMore().finally(function() {
                    setPullUpStatus("succeed");
                    bScroll.current.finishPullUp();
                });
            });
        }
        if (isMobile() && resetProps.pullDownRefresh && reload) {
            bScroll.current.on("pullingDown", function() {
                setPullDownStatus("fetching");
                reload().finally(function() {
                    setPullDownStatus("succeed");
                    bScroll.current.finishPullDown();
                });
            });
            bScroll.current.on("enterThreshold", function() {
                setPullDownStatus("enter");
            });
            bScroll.current.on("leaveThreshold", function() {
                setPullDownStatus("leave");
            });
        }
    };
    var size = useSize($content);
    useMount(function() {
        init();
    });
    useUnmount(function() {
        var _bScroll_current;
        (_bScroll_current = bScroll.current) === null || _bScroll_current === void 0 ? void 0 : _bScroll_current.destroy();
    });
    useUpdateEffect(function() {
        var _bScroll_current;
        (_bScroll_current = bScroll.current) === null || _bScroll_current === void 0 ? void 0 : _bScroll_current.refresh();
    }, [
        size === null || size === void 0 ? void 0 : size.height,
        size === null || size === void 0 ? void 0 : size.width
    ]);
    return /*#__PURE__*/ _jsx("div", {
        ref: $wrapper,
        className: classNames(classes.scroll_view, className),
        children: /*#__PURE__*/ _jsxs("div", {
            ref: $content,
            className: classNames(wrapperClassName, isMobile() || scrollX ? classes.content : classes.padding),
            children: [
                resetProps.pullDownRefresh && (renderPullDownTip === null || renderPullDownTip === void 0 ? void 0 : renderPullDownTip(pullDownStatus, defaultPullDownTipRender(pullDownStatus))),
                props.children,
                resetProps.pullUpLoad && !noMore && (renderPullUpTip === null || renderPullUpTip === void 0 ? void 0 : renderPullUpTip(pullUpStatus, defaultPullUpTipRender(pullUpStatus)))
            ]
        })
    });
}
export default ScrollView;

 //# sourceMappingURL=index.js.map